<script setup>
import {Iphone, Lock, User} from "@element-plus/icons-vue";
import {ref} from "vue";
import {userResetPassword} from "@/api/user.js";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";

// 判断该渲染哪个组件
const isRegister = defineModel()
// 表单数据收集
const formModel = ref({
  username: '',
  password: '',
  repassword: '',
  phone: ''
})
// 校验规则
const rules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    {required: true, message: '请再次输入密码', trigger: 'blur'},
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  phone: [
    {required: true, message: '请输入手机号', trigger: 'blur'},
    {
      pattern: /^1[3-9]\d{9}$/,
      message: '请输入正确的手机号',
      trigger: 'blur'
    }
  ]
}
// 表单对象
const form = ref()
// 重置密码方法
const router = useRouter()
const reset = async () => {
  await form.value.validate()
  await userResetPassword(
      formModel.value.username,
      formModel.value.password,
      formModel.value.phone
  )
  ElMessage.success("重置密码成功")
  isRegister.value = 1
}
</script>

<template>
  <el-form
      :model="formModel"
      :rules="rules"
      ref="form"
      size="large"
      autocomplete="off"
      @keyup.enter="reset"
  >
    <el-form-item>
      <h1>忘记密码</h1>
    </el-form-item>
    <!--    用户名-->
    <el-form-item prop="username">
      <el-input
          v-model="formModel.username"
          :prefix-icon="User"
          placeholder="请输入用户名"></el-input>
    </el-form-item>
    <!--    密码-->
    <el-form-item prop="password">
      <el-input
          v-model="formModel.password"
          :prefix-icon="Lock"
          type="password"
          placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <!--      再次确认密码-->
    <el-form-item prop="repassword">
      <el-input
          v-model="formModel.repassword"
          :prefix-icon="Lock"
          type="password"
          placeholder="请输入再次密码"
      ></el-input>
    </el-form-item>
    <!--    电话-->
    <el-form-item prop="phone">
      <el-input
          v-model="formModel.phone"
          :prefix-icon="Iphone"
          placeholder="请输入手机号"
      >
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button
          @click="reset"
          class="button"
          type="primary"
          auto-insert-space>
        重置密码
      </el-button>
    </el-form-item>
    <el-form-item class="flex">
      <el-link type="info" :underline="false" @click="isRegister = 1">
        ← 返回
      </el-link>
    </el-form-item>
  </el-form>
</template>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  user-select: none;

  .title {
    margin: 0 auto;
  }

  .button {
    width: 100%;
  }

  .flex {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
}
</style>
